import { defineSource } from '../source';
import { ProcessedItem } from '../types'
import { myFetch } from "../utils/fetch"
import logger from '../../framework/config/logger';

interface Item {
    uri: string
    id: string
    title?: string
    content_text: string
    content_short: string
    display_time: number
    type?: string
  }

interface NewsRes {
    data: {
      items: {
        // ad
        resource_type?: string
        resource: Item
      }[]
    }
  }

/**
 * 排行榜 
 */
export default defineSource({
    wallstreetcn: async (): Promise<ProcessedItem[]> => {
        const apiUrl = `https://api-one.wallstcn.com/apiv1/content/information-flow?channel=global-channel&accept=article&limit=30`
      
        const res: NewsRes = await myFetch(apiUrl)
        return res.data.items
          .filter(k => k.resource_type !== "theme" && k.resource_type !== "ad" && k.resource.type !== "live" && k.resource.uri)
          .map(({ resource: h }) => {
            return {
              id: h.id,
              title: h.title || h.content_short,
              url: h.uri,
              date: String(h.display_time * 1000),
              extra: {
                content: h.content_short
              },
            }
          })
      }
});
